package model;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;

/**
 *
 * @author Misja
 */
public class Enemy extends Part {

    public Enemy() {
        super.setMovable(false);

        try {
            URL url = this.getClass().getResource("enemylevel1.gif");
            super.setAppereance(ImageIO.read(url));

        } catch (IOException ex) {
        }
    }

    public String moveEnemy() {

        ArrayList<Field> validOptions = new ArrayList<>();
        Field neighborField = null;
        Random random = new Random();

        for (int i = 0; i < 4; i++) {

            switch (i) {
                case 0:
                    neighborField = this.currentField.getNeighbor("LEFT");
                    break;
                case 1:
                    neighborField = this.currentField.getNeighbor("RIGHT");
                    break;
                case 2:
                    neighborField = this.currentField.getNeighbor("UP");
                    break;
                case 3:
                    neighborField = this.currentField.getNeighbor("DOWN");
                    break;
            }

            if (neighborField != null) {
                if (neighborField.getPart() instanceof Player){
                    currentField.setPart(null);
                    neighborField.setPart(this);                    
                    //setCurrentField(neighborField);
                    return "ATEPLAYER";
                } else if (neighborField.getPart() == null) {
                    validOptions.add(neighborField);
                }
            }
        }

        if (validOptions.size() > 0) {
            int x = random.nextInt((validOptions.size()));
            Field neighbor = validOptions.get(x);
            currentField.setPart(null);
            neighbor.setPart(this);
            
            //setCurrentField(neighbor);
            return "MOVED";
        }

        return "CANNOTMOVE";
    }
}